<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Classes\UploadImg;
use App\Http\Controllers\AdminController;
use App\Models\Shop\Slide;
use App\Models\Shop\Store;
use Carbon\Carbon;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\DB;

class SlideController extends AdminController
{
    /**
     * 验证条件数组
     * @var array
     */
    protected $verifyFactor = [
        'title'             =>  'required',
        'thumb'             =>  'required',
        'storeID'           =>  'required'
    ];

    public function getIndex(Request $request)
    {
        $slide = new Slide();
        if($request->has('storeID'))
        {
            $slide = $slide->where('storeID', $request->input('storeID'));
        }
        $slide = $slide->orderBy('id', 'desc')->paginate(20);
        $store = Store::orderBy('id', 'desc')->get()->keyBy('id');
        return view('admin.shop.slide')->with(['slide' => $slide, 'store' => $store]);
    }

    public function getAdd()
    {
        $store = Store::orderBy('id', 'desc')->get()->keyBy('id');
        return view('admin.shop.slide-add')->with(['store' => $store]);
    }
    public function postAdd(Request $request)
    {
        //验证
        $verifyMsg = $this->verification($request->input());
        if ($verifyMsg)
        {
            return redirect()->action('Admin\Shop\SlideController@getAdd')->withInput()->withErrors($verifyMsg);
        }
        $display = 0;
        if($request->has('display'))
            $display = 1;
        $data = [
            'title'         =>  $request->input('title'),
            'thumb'         =>  $request->input('thumb'),
            'storeID'       =>  $request->input('storeID'),
            'sort'          =>  0,
            'display'       =>  $display,
            'link'          =>  $request->input('link'),
            'created_ad'    =>  Carbon::now()
        ];
        //Create
        if (DB::table('shop_slide')->insert($data))
        {
            //setLog
//            $this->setLog('create', NULL, json_encode($data));
            return ShowMsg('success', getLang('Tips', 'createSuccess'), action('Admin\Shop\SlideController@getIndex'), 1);
        }

        return ShowMsg('error', getLang('Tips', 'createFail'), action('Admin\Shop\SlideController@getIndex'), 1);
    }

    public function getEdit($id)
    {
        //获取幻灯
        $slide = DB::table('shop_slide')->where('id', $id)->first();
        $store = Store::orderBy('id', 'desc')->get()->keyBy('id');
        return view('admin.shop.slide-edit')->with(['slide' => $slide, 'store' => $store]);
    }
    public function postEdit(Request $request, $id)
    {
        //验证
        $verifyMsg = $this->verification($request->input());
        if ($verifyMsg)
        {
            return redirect()->action('Admin\Shop\SlideController@getAdd')->withInput()->withErrors($verifyMsg);
        }
        $display = 0;
        if($request->has('display'))
            $display = 1;
        $data = [
            'title'         =>  $request->input('title'),
            'thumb'         =>  $request->input('thumb'),
            'storeID'       =>  $request->input('storeID'),
            'link'          =>  $request->input('link'),
            'display'       =>  $display
        ];
        //Update
        if (Slide::where('id', $id)->update($data))
        {
            //setLog
//            $this->setLog('update', json_encode($origin), json_encode($data));
            return ShowMsg('success', getLang('Tips', 'updateSuccess'), action('Admin\Shop\SlideController@getEdit', ['id' => $id]), 1);
        }
        return ShowMsg('error', getLang('Tips', 'updateFail'), action('Admin\Shop\SlideController@getEdit', ['id' => $id]), 1);
    }

    public function getDel()
    {}

    /**
     * Ajax是否显示
     * @param Request $request
     * @return string
     */
    public function postDisplay(Request $request)
    {
        $slide = Slide::find($request->input('id'));
        if(!empty($slide))
        {
            $origin = $slide->toArray();
            $slide->display = $request->input('display');
            if ($slide->save())
            {
                //setLog
//                $this->setLog('show', json_encode($origin), json_encode($request->except('_token')));
                return json_encode([
                    'code'      =>  1,
                    'message'   =>  getLang('Tips', 'updateSuccess')
                ]);
            }
        }
        return json_encode([
            'code'      =>  0,
            'message'   =>  getLang('Tips', 'updateFail')
        ]);
    }

    /**
     * 上传缩略图
     * @param UploadImg $uploadImg
     * @param Request $request
     */
    public function postUpload(UploadImg $uploadImg, Request $request)
    {
        if ($request->hasFile('icon'))
        {
            if ($request->file('icon')->isValid())
            {
                $avatar_file = $request->file('icon');
                $size = getimagesize($avatar_file);
                //"{"x":8.000000000000002,"y":8.000000000000002,"height":64,"width":64,"rotate":0}"
                $avatar_data = json_encode([
                    'x' =>  0,
                    'y' =>  0,
                    'height'    =>  $size[1],
                    'width'     =>  $size[0],
                    'rotate'    =>  0
                ]);
                $avatar_data_obj = json_decode(stripslashes($avatar_data));
                $date = date('Y-m-d', time());
                $uploadImg->savePath = config('imgUpdate.updatePath').$date.'/';
                //横纵比例
                $prefix = md5(time());
                $aspectRatio = [
                    [
                        'width'     =>  375,
                        'height'    =>  250,
                        'path'      =>  $prefix.'.png'
                    ]
                ];
                $backData = $uploadImg->upThumb($avatar_file, $avatar_data, $aspectRatio);
                if(count($backData) > 0)
                {
                    //返回数据
                    $backData['thumb'] = $date.'.'.$backData['thumb'];
                    $backData['msg'] = '上传成功！';
                    echo json_encode($backData);
                }
            }
        }
    }
}
